import math
from PIL import Image

# fast alle funktionen von vektoren aus 1. vl implementieren
# dann kamera
# 
class Point(object):
    def __init__(self, x=0, y=0, z=0):
        self.x = x
        self.y = y
        self.z = z

    def __sub__(self, p2): # getestet
        """Vektor von self -> p2"""
        x = self.x - p2.x
        y = self.y - p2.y
        z = self.z - p2.z
        return Vektor(x, y, z)

    def __repr__(self):
        return '(%d %d %d)' % (self.x, self.y, self.z)

class Vektor(object):
    """im R3 also 3D Vektoren"""
    def __init__(self, x=0, y=0, z=0):
        if isinstance(x, Point):
            x, y, z = x.x, x.y, x.z            
        self.x = x
        self.y = y
        self.z = z
        
    def length(self): # getestet
        """Laenge des Vektors"""
        l = math.sqrt(self.x**2 + self.y**2 + self.z**2)
        return l
    
    def normalized(self):
        """gibt normalisierten Vektor zurueck"""
        if self.length() == 0:
            return Vektor(0,0,0)
        
        alpha = 1 / self.length()
        print 'len = ', self.length()
        print 'alpha = ', alpha
        if (self.x, self.y, self.z) == (0,0,0):
            return Vektor(0,0,0)
        return Vektor(alpha*self.x, alpha*self.y, alpha*self.z)
        
    def scale(self, skalar): # getestet
        """Skaliert Vektor mit einem Skalar"""
        return Vektor(self.x*skalar, self.y*skalar, self.z*skalar)
        
    def cross(self, oth): # getestet
        """Kreuzprodukt (Vektorprodukt)"""
        x = self.y*oth.z - self.z*oth.y
        y = self.z*oth.x - self.x*oth.z
        z = self.x*oth.y - self.y*oth.x
        return Vektor(x, y , z)

    def dot(self, oth): # getestet
        """Skalarprodukt"""
        return self.x*oth.x + self.y*oth.y + self.z*oth.z
                    
    def __sub__(self, oth): # getestet
        """Vektor Subtraktion"""
        return Vektor(self.x-oth.x, self.y-oth.y, self.z-oth.z)

    def __repr__(self):        
        return '(%d %d %d)' % (self.x, self.y, self.z)

    
    def __mul__(self, oth): # getestet
        typ = type(oth)
        if typ == int:
            return self.scale(oth)
        if typ == Vektor:
            return self.dot(oth)
            
                
class Ray(object):
    def __init__(self, origin, direction):
        self.origin = origin # point
        self.direction = direction.normalized() # vector

    def __repr__(self):
        return 'Ray(%s,%s)' % (repr (self.origin), repr (self.direction))

    def pointAtParameter(self, t):
        return self.origin + self.direction.scale(t)

if __name__ == '__main__':
    print 'start'
    # canvas

    p1 = Point(1,2,3)
    p2 = Point(4,5,6)
    v = p1-p2
    v1, v2 = Vektor(0,3,0), Vektor(p2)
##    skalarprodukt = v1 * v2 #v1.dot(v2)
##    kreuzprodukt = v1.cross(v2)
##    sub = v1 - v2
##    skaliert = v1 * 5
    normiert = v1.normalized()
    
##    print 'p1 p2 = ', p1, p2
##    print 'v = ', v
##    print 'v.length() = ', v.length()
##    print 'v1, v2 = ', v1, v2
##    print 'v1 * v2 = ', skalarprodukt
##    print 'v1.cross(v2) = ', kreuzprodukt
##    print 'v1 - v2 = ', sub
##    print 'v1 * 5  = ', skaliert
    print 'normiert = ', normiert.z
    
    # zeichnen
    im = Image.new('RGB', (320,240), (0,0,255))
##    im.show()
